package com.ruoyi.kong.service.impl;

import com.ruoyi.kong.entity.*;
import com.ruoyi.kong.fegin.KongClientService;
import com.ruoyi.kong.service.IKongRouteInfoService;
import com.ruoyi.kong.utils.KongActiveProfileUtil;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 网关路由 服务层 实现类
 */
@Service
public class KongRouteInfoServiceImpl implements IKongRouteInfoService {

    /**
     *
     */
    @Autowired
    private KongActiveProfileUtil kongActiveProfileUtil;

    /**
     * 返回 路由列表
     * @return
     */
    public KongEntity<KongRoute> getRouteList(){
        KongEntity<KongRoute> routeList = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).getRouteList();
        if(!ObjectUtils.isEmpty(routeList)){
            if(!CollectionUtils.isEmpty(routeList.getData())){
                // 获取服务信息  和 插件信息  并赋值
                for(KongRoute kongRoute:routeList.getData()){
                    String servcieId=kongRoute.getService().getId();
                    KongService _kongService = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).getServiceById(servcieId);
                    if(!ObjectUtils.isEmpty(_kongService)){
                        kongRoute.setKongService(_kongService);
                    }
                    KongEntity<KongPlugin> plugins=KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginsByRouteId(kongRoute.getId());
                    if(!ObjectUtils.isEmpty(plugins)){
                        kongRoute.setKongPlugins(plugins);
                    }
                }
            }
        }
        return routeList;
    }

    /**
     * 新增kongRoute
     * @param kongRoute
     * @return
     */
    public KongRoute addRoute(KongRoute kongRoute){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).addRoute(kongRoute);
    }

    /**
     * 删除路由
     * @param routeId
     */
    public void deleteRoute(String routeId){
        KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).deleteRoute(routeId);
    }


    /**
     * 服务id 返回路由信息
     * @param serviceId
     * @return
     */
    public KongEntity<KongRoute> routeByServiceId( String serviceId){
       return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).routeByServiceId(serviceId);
    }

    public KongRoute routeById(String routeId){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).routeById(routeId);
    }

    public KongRoute updateRoute(String routeId, KongRouteVo kongRoute){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).updateRoute(routeId,kongRoute);
    }

    public KongEntity<KongPlugin> pluginsByRouteId(String routeId){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginsByRouteId(routeId);
    }

    public KongEntity<KongPlugin> pluginsByServiceId(String serviceId){
        return  KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginsByServiceId(serviceId);
    }

}
